package peace.util
{
import flash.events.ContextMenuEvent;
import flash.events.Event;
import flash.net.LocalConnection;
import flash.system.Security;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;

import peace.gui.controls.text.InputField;
/**
 * Примитивная консоль для peace.util.Logger
 *  
 * @author peace
 * 
 * @example
 <listing>	
 	private var debugger:DebugConsole = new DebugConsole();
	private function initDebug():void
	{
		debugger.width = debugger.height = 300;
		debugger.alpha = 0.7;

		Logger.setEnabled(true, true);
		stage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void
		{
			if (e.keyCode == 192 && e.ctrlKey) // ` or ~
			{
				if (debugger.parent) stage.removeChild(debugger);
				else stage.addChild(debugger);
			}
		});
	}
</listing>
 * 
 */
public class DebugConsole extends InputField
{
	/**
	 * 
	 */
	private var lc:LocalConnection;
	/**
	 * 
	 * 
	 */	
	public function DebugConsole(useLocalConnection:Boolean = true)
	{
		super(null, true);
		
		if (useLocalConnection)
		{
			lc = new LocalConnection();
			Security.allowDomain("*");
			lc.allowDomain("*");
			lc.client = this;
			try {lc.connect("LoggerOutput");} catch (error:Error){}
		}
		
		
		var i:ContextMenuItem = new ContextMenuItem("CLEAR", true);
		i.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, clearLog, false,0,true);
		var m:ContextMenu = new ContextMenu();
		m.hideBuiltInItems();
		m.customItems.push(i);
		contextMenu = m;
	}
	/**
	 * Приемник сообщенийы
	 * 
	 * @param s
	 * 
	 */	
	public function log (s:String):void
	{
		textField.appendText (s + "\n");
	}
	/**
	 * 
	 * 
	 */	
	public function clearLog (e:Event = null):void
	{
		textField.text = "";
	}
	
	
}
}